// 畅龙 2008
var XForm;
(function(){
	var beforValidate	= function(){
		if( this.isSubmit ) {
			alert('表单已经递交!');
			return true;
		}
		
		var submit	= window[ this.dom.id + '_submit'];
		if( typeof submit == 'function' ){
			try{
				if( !submit.call(this.dom) )
					return true;
			}catch(e){}
		}
	}
	var afterValidate	= function(){
		var submit	= window[ this.dom.id + '_submit1'];
		if( typeof submit == 'function' ){
			try{
				if( !submit.call(this.dom) )
					return true;
			}catch(e){}
		}
		this.isSubmit	= true;
	}
	var AddFormEvent	= function(form){
		// handle depend elements
		form.getElements('tr[depends]').each(function(tr){
			tr.retrieve('__display', 'none');
			var o	= JSON.decode('{'+tr.getAttribute('depends')+'}');
			tr.setAttribute('depends', '');
			$each(o, function(o, name){
				var from	= form.getElements('*[name='+name+']');
				from[0].getParent('tr').addEvent('display', function(){
					var display	= this.getStyle('display');
					if(  display === 'none' ){
						tr.setStyle('display', 'none');
					}else{
						tr.setStyle('display', tr.retrieve('__display'));
					}
					tr.fireEvent('display');
				});
				var _o	= o.map(function(e){
					return String(e);
				});
				if(from[0].tagName == 'SELECT' ){
					from[0].addEvent('change', (function(evt){					
						tr.setStyle('display', _o.indexOf(this.value) >=0 ? '':'none');
						tr.store('__display', tr.getStyle('display'));
						tr.fireEvent('display');
						return arguments.callee;
					}).call(from[0]));
				}else{
					from.addEvent('click', (function(){
						tr.setStyle('display', from.some(function(el){
							return (el.checked && _o.indexOf(el.value) >=0);
						}) ? '' : 'none');
						tr.store('__display', tr.getStyle('display'));
						tr.fireEvent('display');
						return arguments.callee;
					})());
				}
				
			});
		});
		// handle associate select
		form.getElements("tr[depField] select").each(function(el){
			var sel	=  $(form.elements[el.getParent('tr').getProperty("depField")]);
			sel.addEvent('change', (function(evt){
				if( ! this.value ){
					el.empty();
					return arguments.callee;
				}
				var did	= parseInt(el.getProperty("val"));
				var url	=  typeof _RootDir == 'string' ?  _RootDir : '.';
				var url	=  url +  "/_admin/param.php?id=" + this.value ;
				//alert(url)
				new Request({
					url: url,
					method: 'get',
					onSuccess: function(txt){
						var o	= JSON.decode(txt);
						el.empty();
						for(var i in o){
							new Element("option", {'value':i, text:  o[i]}).inject(el);
						}
						if( did && o[did] ) {
							el.getElement("option[value="+did+"]")
								.setStyle("color", "#f00")
									.setAttribute("selected",  "selected");
						}
						el.fireEvent('change');
					}
				}).send();
				return arguments.callee;
			}).call(sel));
		});
		//handel val
		form.getElements("tr[val]").each(function(tr){
			var val = tr.getAttribute('val').trim().split(/\s*,\s*/).filter(function(val){
				return 	val !== '';
			}).map(function(val){
				return 	 "input[value="+val+"]" ;
			});
			if( !val.length ){
				return ;
			}
			var inputs	=tr.getElements( val.join(',') );
			var sel	= function(){
				inputs.each(function(el){
					el.checked	= true;
				});
			}
			form.addEvent('reset', (function(){
				setTimeout(sel, 20);
				return arguments.callee
			})());
		});
	}
	XForm = new Class({
		id: null,
		dom: null,
		isSubmit: false,

		initialize: function(id, callback){
			this.id		= id;
			this.dom	=  $(id).getParent('form');
			if( !this.dom ){
				return false;
			}
			window.addEvent('domready', function(){
				AddFormEvent(this.dom);
				this.dom.addEvent('submit', function(evt){
					var funs = [beforValidate, callback, afterValidate];
					for(var i = 0;i<funs.length;i++){
						if( funs[i].call(this) ){
							evt.stop();
							return false;
						}
					}
				}.bind(this));
			}.bind(this));
		}

	});
})();



